﻿#include "threadpool.h"

#include <iostream>
#include <thread>
#include <chrono>
#include <memory>

class MyTask : public Task
{
public:
	MyTask(int begin, int end)
		: begin_(begin)
		, end_(end)
	{}

	Any run()
	{
		std::cout << "begin threadFunc()  tid:" << std::this_thread::get_id() << std::endl;
		std::cout << std::this_thread::get_id() << std::endl;
		
		long long sum = 0;
		for (long long i = begin_; i <= end_; ++i)
		{
			sum += i;
		}
		std::cout << "end threadFunc()  tid:" << std::this_thread::get_id() << std::endl;
		return sum;
	}
private:
	int begin_;
	int end_;
};


//int main()
//{
//	{
//		ThreadPool pool;
//		pool.setMode(PoolMode::MODE_CACHED);
//		pool.setThreadMaxThreshHold(64);
//		pool.start(4);
//
//		Result res1 = pool.submitTask(std::make_shared<MyTask>(1, 100000000));
//		Result res2 = pool.submitTask(std::make_shared<MyTask>(100000001, 200000000));
//		Result res3 = pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
//		pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
//		pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
//		pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
//
//		 std::cout << 
//	}
//
//
//	getchar();
//
//	return 0;
//}

int main()
{
	{
		ThreadPool pool;
		//pool.setMode(PoolMode::MODE_FIXED);
		pool.start(4);
		Result res1 = pool.submitTask(std::make_shared<MyTask>(1, 100000000));
		Result res2 = pool.submitTask(std::make_shared<MyTask>(100000001, 200000000));
		Result res3 = pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));

		long long sum1 = res1.get().cast_<long long>();
		long long sum2 = res2.get().cast_<long long>();
		long long sum3 = res3.get().cast_<long long>();

		std::cout << sum1 + sum2 + sum3 << std::endl;

		std::cout << "开始析构线程池" << std::endl;
	}

	std::cout << "this place exit threadpool" << std::endl;
	getchar();
	return 0;
}


//int main()
//{
//	{
//		ThreadPool pool;
//		pool.setMode(PoolMode::MODE_CACHED);
//		pool.start(4);
//		Result res1 = pool.submitTask(std::make_shared<MyTask>(1, 100000000));
//		Result res2 = pool.submitTask(std::make_shared<MyTask>(100000001, 200000000));
//		Result res3 = pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
//		pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
//		pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
//		pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
//
//		long long sum1 = res1.get().cast_<long long>();
//		long long sum2 = res2.get().cast_<long long>();
//		long long sum3 = res3.get().cast_<long long>();
//
//		std::cout << sum1 + sum2 + sum3 << std::endl;
//
//		// std::cout << "开始析构线程池" << std::endl;
//	}
//	
//	std::cout << "this place exit threadpool" << std::endl;
//	getchar();
//	return 0;
//}


//int main()
//{
//	ThreadPool pool;
//	pool.start(4);
//	pool.submitTask(std::make_shared<MyTask>());
//	pool.submitTask(std::make_shared<MyTask>());
//	pool.submitTask(std::make_shared<MyTask>());
//	pool.submitTask(std::make_shared<MyTask>());
//	pool.submitTask(std::make_shared<MyTask>());
//	pool.submitTask(std::make_shared<MyTask>());
//	pool.submitTask(std::make_shared<MyTask>());
//	pool.submitTask(std::make_shared<MyTask>());
//	pool.submitTask(std::make_shared<MyTask>());
//
//	getchar();
//	return 0;
//}


//int main()
//{
//	ThreadPool tp;
//	tp.start(6);
//
//	std::this_thread::sleep_for(std::chrono::seconds(5));
//
//	// getchar();
//}